<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script src="js/three.js"></script>
    <script src="js/OrbitControls.js"></script>
    <script src="js/libs/stats.min.js"></script>
    <script src="js/libs/ThreeBSP.js"></script>
    <script src="js/loader/DDSLoader.js"></script>
    <script src="js/loader/MTLLoader.js"></script>
    <script src="js/loader/OBJLoader.js"></script>
</head>
<body>

<div id="container"></div>
<script>

    var stats;
    var container
    var camera, controls, scene, renderer,INTERSECTED;
    var raycaster = new THREE.Raycaster();
    var mouse = new THREE.Vector2();
    var onClickPosition = new THREE.Vector2();
    var closed = -0.01;
    var opened = -(Math.PI/2-0.01);
    var light,light2,lightHelper;
    init();
    animate(); // remove when using next line for animation loop (requestAnimationFrame)
    //animate();

    function init() {

        scene = new THREE.Scene();

        renderer = new THREE.WebGLRenderer();
        renderer.setClearColor( 0xffffff);
        renderer.setPixelRatio( window.devicePixelRatio );
        renderer.setSize( window.innerWidth, window.innerHeight );
        renderer.shadowMap.enabled=true;

        container = document.getElementById( 'container' );
        container.appendChild( renderer.domElement );

        camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
        camera.position.z = 2000;

        controls = new THREE.OrbitControls( camera, renderer.domElement );
        controls.addEventListener( 'change', render ); // remove when using animation loop
        // enable animation loop when using damping or autorotation
        //controls.enableDamping = true;
        //controls.dampingFactor = 0.25;
        controls.enableZoom = true;

        // world

        var textureLoader = new THREE.TextureLoader();

        //geometry part

        var floorGeo = new THREE.CubeGeometry(1600,1300,1);
        var floorMat ;
        var floorMesh;

        textureLoader.load('model/texture/floor.png', function(tex) {
            floorMat = new THREE.MeshLambertMaterial({map:tex, color:0xBEC9BE, emissive:0xBEC9BE,emissiveIntensity:0.005, overdraw: false});
            floorMat.map.wrapS = THREE.RepeatWrapping;
            floorMat.map.wrapT = THREE.RepeatWrapping;
            floorMat.map.repeat.set(13,10);
            floorMesh = new THREE.Mesh(floorGeo,floorMat);
            floorMesh.position.x=600;
            floorMesh.position.y=450;
            floorMesh.receiveShadow=true;
            scene.add(floorMesh);
        });





        var shape = new THREE.Shape();
        shape.moveTo(0, 0);
        shape.lineTo(0, 30);
        shape.lineTo(30, 270);
        shape.lineTo(0, 270);
        shape.lineTo(0,0);
        var outShape=new THREE.Shape();
        outShape.moveTo(-30, -30);
        outShape.lineTo(1030, -30);
        outShape.lineTo(1030, 530);
        outShape.lineTo(530, 530);
        outShape.lineTo(530, 1030);
        outShape.lineTo(-30, 1030);
        outShape.lineTo(-30, -30);
        var options = {
            amount: 275
        };

        var wallGeo = new THREE.ExtrudeGeometry(shape,options);
        var wallMat = new THREE.MeshBasicMaterial({color:0x002288});
        var wallMesh = new THREE.Mesh(wallGeo,wallMat);
        var outwallGeo = new THREE.ExtrudeGeometry(outShape,options);
        var outwallMat = new THREE.MeshBasicMaterial({color:0x002288});
        var outwallMesh = new THREE.Mesh(outwallGeo,outwallMat);

        var doorGeo1 = new THREE.CubeGeometry(150,90,200);
        var doorMat1 = new THREE.MeshBasicMaterial({color:0xff0000});
        var door1    = new THREE.Mesh(doorGeo1,doorMat1);
        door1.position.x = 100;
        door1.position.z = 105;


        var winGeo1 = new THREE.CubeGeometry(600,80,150);
        var winMat1 = new THREE.MeshBasicMaterial({color:0xff0000});
        var win1    = new THREE.Mesh(winGeo1,winMat1);
        win1.position.x = 600;
        win1.position.z = 145;

        var wallBSP = new ThreeBSP(wallMesh);
        var outwallBSP = new ThreeBSP(outwallMesh);
        var door1BSP = new ThreeBSP(door1);
        var win1BSP = new ThreeBSP(win1);
        var resultBSP = outwallBSP.subtract(wallBSP);
        resultBSP = resultBSP.subtract(door1BSP).subtract(win1BSP);

        var result = resultBSP.toMesh(new THREE.MeshLambertMaterial({
            shading: THREE.SmoothShading,
            color: 0xB0C4e6}));
        var result = resultBSP.toMesh();
        result.castShadow=true;
        result.name="wall";

        var map = THREE.ImageUtils.loadTexture('model/texture/outside_lightmap.jpg');
        var faces = result.geometry.faces;

        result.geometry.faceVertexUvs[0] = [];
        console.log("length:"+faces.length);
        for (var i = 0; i < faces.length ; i++) {

            var v1 = result.geometry.vertices[faces[i].a],
                    v2 = result.geometry.vertices[faces[i].b],
                    v3 = result.geometry.vertices[faces[i].c];
            console.log("length:"+v1.x+"-"+v2.x+"-"+v3.x);

            if(v1.x<0 && v2.x<0 && v3.x<0){
                result.geometry.faceVertexUvs[0].push([
                    new THREE.Vector2(v1.y/1000 ,v1.z/275),
                    new THREE.Vector2(v2.y/1000 ,v2.z/275),
                    new THREE.Vector2(v3.y/1000 ,v3.z/275)
                ]);
                console.log(1111);
            }else{
                result.geometry.faceVertexUvs[0].push([
                    new THREE.Vector2(0,0),
                    new THREE.Vector2(0,0),
                    new THREE.Vector2(0,0)
                ]);
            }

        }
        result.geometry.uvsNeedUpdate = true;
        var ss = new THREE.Mesh(result.geometry,new THREE.MeshLambertMaterial({
            color:0xBEC9BE,
            map:map
        }));
        ss.name="11";
        scene.add(ss);






        light2 = new THREE.AmbientLight( 0xffffff );
        light2.intensity=0.8;
        scene.add( light2 );

        light = new THREE.DirectionalLight(0xffffff);
        light.intensity=0.5;
        light.castShadow = true;
        light.position.set( 500, 500, 1000 );

        var tar1 = new THREE.Object3D();
//         lightHelper= new THREE.SpotLightHelper(light);
        tar1.position.set(500,500,0);

        tar1.name="111";
        scene.add(tar1);

        console.log(tar1);
        light.target=tar1;

        scene.add( light );

//        scene.add( lightHelper );

        //

        stats = new Stats();
        container.appendChild( stats.dom );

        //

        window.addEventListener( 'resize', onWindowResize, false );
        container.addEventListener( 'mousemove', onMouseMove, false );
//        container.addEventListener("dblclick",openClose,false);
    }

    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();

        renderer.setSize( window.innerWidth, window.innerHeight );

    }

    function animate() {

        requestAnimationFrame( animate );

        controls.update(); // required if controls.enableDamping = true, or if controls.autoRotate = true

        stats.update();

        render();

    }

    function render() {

        renderer.render( scene, camera );
        doorSwitch(needOpen);
    }



</script>
</body>
</html>